package com.itheima.d3_thread_safe;

class Ticket extends Thread {
    private int ticket = 100;

    @Override
    public void run() {
        while (true) {
            synchronized (Thread.class) {
                if (ticket > 0) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }

                    ticket--;
                    System.out.println(Thread.currentThread().getName() + " 在卖票，剩余票数为" + ticket);
                } else {
                    break;
                }
            }
        }
    }
}


